package com.hoangqp.android.stardictcore;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.content.Context;

import com.hoangqp.android.database.MyDict;
import com.hoangqp.stardict.parse.WrongPropertyException;

public class StarDictParser {

	private Context context;

	/**
	 * folder that contains dict files
	 */
	private String dictFilesFolder;

	/**
	 * dict informations
	 */
	// private StarDictInfo dictInfo;

	private IfoFile ifoFile;
	private IdxFile idxFile;
	private StarDictDictFileParser dictFileParser;

	/**
	 * 
	 * @param dictFileFolder
	 * @throws WrongPropertyException
	 * @throws IOException
	 */
	public StarDictParser(String dictFilesFolder) throws IOException,
			WrongPropertyException {
		this.dictFilesFolder = dictFilesFolder;
		checkDictFilesFolderValid();
		initStarDictParser();
	}

	public StarDictParser(String dictFilesFolder, Context context) throws IOException,
			WrongPropertyException {
		this.dictFilesFolder = dictFilesFolder;
		this.context = context;
		checkDictFilesFolderValid();
		initStarDictParser();
	}

	private void checkDictFilesFolderValid() throws FileNotFoundException {
		File f = new File(dictFilesFolder);
		if (!(f.exists() && f.isDirectory())) {
			throw new FileNotFoundException("Dict folder " + dictFilesFolder
					+ " could not be found!");
		}
	}

	private void initStarDictParser() throws IOException,
			WrongPropertyException {
		// init infoFileParser

		File file = new File(dictFilesFolder);
		String[] list = file.list();
		// Map<file extension,file name>
		Map<String, String> fileMap = new HashMap<String, String>();

		for (int i = list.length - 1; i >= 0; i--) {
			if (list[i].endsWith(".ifo")) {
				fileMap.put(".ifo", list[i]);
			} else if (list[i].endsWith(".idx")) {
				fileMap.put(".idx", list[i]);
			} else if (list[i].endsWith(".dict")) {
				fileMap.put(".dict", list[i]);
			} else if (list[i].endsWith(".dz") && list[i].contains(".dict.dz")) {
				fileMap.put(".dict.dz", list[i]);
			}
		}

		this.ifoFile = new IfoFile(dictFilesFolder + File.separator
				+ fileMap.get(".ifo"));
		// fileMap.remove(fileMap.get(".ifo"));

		String pathFile = null;
		if (fileMap.get(".dict") != null) {
			pathFile = dictFilesFolder + File.separator + fileMap.get(".dict");
		} else {
			pathFile = dictFilesFolder + File.separator
					+ fileMap.get(".dict.dz");
		}
		this.dictFileParser = new StarDictDictFileParser(pathFile);

		if (fileMap.get(".idx") != null) {
			pathFile = dictFilesFolder + File.separator + fileMap.get(".idx");
		} else {
			pathFile = dictFilesFolder + File.separator
					+ fileMap.get(".idx.gz");
		}
		this.idxFile = new IdxFile(pathFile, ifoFile.getLongWordCount(),
				ifoFile.getLongIdxFileSize(), context);
	}

	/**
	 * get words count
	 */

	public long getWordsCount() {
		return ifoFile.getLongWordCount();
	}

	/**
	 * get book name of dictionary.
	 * 
	 * @return Book name
	 */
	public String getDictName() {
		return ifoFile.getStrBookname().replace("\r", "").trim();
	}

	/**
	 * get book version.
	 * 
	 * @return version of a dictionary
	 */
	public String getDictVersion() {
		return ifoFile.getStrVersion();
	}

	/**
	 * get amount of words in a StarDict dictionary (within 3 files).
	 * 
	 * @return a long totalWord.
	 */
	public int getTotalWords() {
		return getWordEntry().size();
	}

	/**
	 * get a list of word entry.
	 * 
	 * @return list of word entry
	 */
	public List<WordEntry> getWordEntry() {
		return idxFile.getEntryList();
	}

	public String getStringSameTypeSequence() {
		return ifoFile.getStrSameTypeSequence();
	}

	/**
	 * get word datas of given word
	 * 
	 * @param word
	 *            given word
	 * @return list of word data
	 * @throws IOException
	 * @throws FileNotFoundException
	 */
	public List<WordDataItem> getWordDatas(WordEntry item)
			throws FileNotFoundException, IOException {

		//Log.d("Info", ifoFile.getStrSameTypeSequence());

		return dictFileParser.getWordDatas(item.getLongOffset(), item.getLongSize(),
				getStringSameTypeSequence());
		/*
		 * if (item.getDataInfo() == null) return new ArrayList<WordDataItem>();
		 * else return dictFileParser.getWordDatas(item.getOffset(),
		 * item.getSize(), getStringSameTypeSequence());
		 */
	}

	public List<WordDataItem> getWordDatas(MyDict item)
			throws FileNotFoundException, IOException {

		//Log.d("Info", ifoFile.getStrSameTypeSequence());

		return dictFileParser.getWordDatas(item.get_offSet(), item.get_size(),
				getStringSameTypeSequence());
		/*
		 * if (item.getDataInfo() == null) return new ArrayList<WordDataItem>();
		 * else return dictFileParser.getWordDatas(item.getOffset(),
		 * item.getSize(), getStringSameTypeSequence());
		 */
	}

}
